package com.example.h5server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.h5server.model.User;
import com.example.h5server.service.IUserService;
import com.example.h5server.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户主表 前端控制器
 * </p>
 *
 * @author 纸鸽
 * @since 2025-10-23
 */
@RestController
@RequestMapping("/user")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Tag(name = "用户接口")
public class UserController {
    @Autowired
    private IUserService userService;
    //查询所有用户
    @GetMapping("/getAllUser")
    @Operation(summary = "查询所有用户")
    public Result<List<User>> getAllUsers() {
        List<User> users = userService.list();
        return Result.success("查询成功", users) ;
    }
    /**
     * 根据用户ID查询用户信息
     * @param id
     * @return
     * @Operation(summary = "根据用户ID查询用户信息")
     */
    @GetMapping("/getUserById")
    @Operation(summary = "根据用户ID查询用户信息")
    public Result<User> getUserById(Long id) {
        User user = userService.getById(id);
        if (user != null) {
            return Result.success("查询成功", user);
        } else {
            return Result.error("用户信息不存在");
        }
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/updateUser")
    @Operation(summary = "修改用户信息")
    public Result<User> updateUser(@RequestBody User user) {
            // 更新密码时进行加密
            if (user.getPassword() != null && !user.getPassword().isEmpty()) {
                user.setPassword(BCrypt.hashpw(user.getMobile(), BCrypt.gensalt()));
            }
            boolean updated = userService.updateById(user);
            if (updated) {
                return Result.success("用户信息更新成功", user);
            } else {
                return Result.error("用户信息更新失败");
            }
        }
        /**
         * 删除用户
         */
    @PostMapping("/deleteUser")
    @Operation(summary = "删除用户")
    public Result<String> deleteUser(Long id) {
            boolean deleted = userService.removeById(id);
            if (deleted) {
                return Result.success("用户删除成功");
            } else {
                return Result.error("用户删除失败");
            }
        }

    @PostMapping("/addUser")
    @Operation(summary = "添加用户")
    public Result<User> addUser(@RequestBody User user) {
        if (user == null) {
            return Result.error("用户信息不能为空");
        }
        //进行加密
        user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
        boolean result = userService.save(user);
        return result ? Result.success("添加成功",user) : Result.error("添加失败");
    }

    @PostMapping("/getByPhone")
    @Operation(summary = "根据手机号查询用户")
    public Result<User> getByPhone( String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", phone);
        User user = userService.getOne(queryWrapper, false);
        if (user != null) {
            return Result.success("查询成功", user);
        } else {
            return Result.error("用户不存在");
        }
    }
}
